
var xmlDom = null;
var xmlDoc = null;
var allNodes = null;
var allArrows = null;
var allTexts = null;

var svgNamespace = 'http://www.w3.org/2000/svg';
var xlinkNamespace = 'http://www.w3.org/1999/xlink';
var SVGDocument = null;
var SVGRoot = null;
var TrueCoords = null;
var GrabPoint = null;
var BackDrop = null;
var DragTarget = null;

var arrowFlag = false;
var currentArrow = null;
var currentElementId = null;

var addLineFirstTargetId = '';

var halfLong = 30; //节点一半长度
var oneTextPx = 14; //一个文件字大概的像素

var idSeq = 0;

var arrowClass = "arrow";
var nodeClass = "node";

var addPointIndex = 0;

//添加方法。为了兼容FireFox、Chrome。
if (!window.ActiveXObject)
{
    XMLDocument.prototype.selectSingleNode = Element.prototype.selectSingleNode = function(xpath)
    {
        var  x = this.selectNodes(xpath);
        if ( ! x || x.length < 1 ) return   null ;
        return  x[0];
    };
	XMLDocument.prototype.selectNodes = Element.prototype.selectNodes = function (xpath)
	{
    	var xpe = new  XPathEvaluator();
        var nsResolver  =  xpe.createNSResolver( this.ownerDocument == null?this.documentElement : this.ownerDocument.documentElement);
        var result = xpe.evaluate(xpath, this, nsResolver, 0 , null);
        var found = [];
        var res;
        while(res = result.iterateNext())
        	found.push(res);
        return found;
	};
}

//加载XML文件并返回XML文档节点
function loadXmlFile(xmlFile){
	var xmlDom = null;
	if (window.ActiveXObject){
		xmlDom = new ActiveXObject('Microsoft.XMLDOM');
		xmlDom.loadXML(xmlFile);//如果用的是XML字符串
		//xmlDom.load(xmlFile);//如果用的是xml文件。
	}else if (document.implementation && document.implementation.createDocument){
		var parseXml = new DOMParser();
		xmlDom = parseXml.parseFromString(xmlFile, 'text/xml');
	}else{
		xmlDom = null;
	}
	return xmlDom;
}


function getParent()
{
	return window.parent;
}

function init(evt)
{
	SVGDocument = evt.target.ownerDocument;
	SVGRoot = SVGDocument.documentElement;
	SVGDocument.window = window;
	TrueCoords = SVGRoot.createSVGPoint();
	GrabPoint = SVGRoot.createSVGPoint();

	BackDrop = SVGDocument.getElementById('BackDrop');
	initFlow();
}

function initShow(evt)
{
	init(evt);
	var history = getParent().document.getElementsByName('statusHistory');
	var len = history.length;
	//初始化arrow
	for(var i = 0; i < len; i++)
	{
		var hx = history[i];
		var from = hx.value.split('|')[0];
		var to = hx.value.split('|')[1];
		var arrowx = getOneElement('/flow/arrows/lineArrow[@from="'+from+'"][@to="'+to+'"]');
		if(arrowx != null)
		{
			var currentEle = SVGDocument.getElementById(arrowx.getAttribute('id'));
			currentEle.setAttribute('marker-end', 'url(#arrawSelect)');
			currentEle.setAttribute('stroke', 'lightgreen');
		}
	}
	
	//初始化node
	var nodeId = getParent().document.getElementById('nodeId');
	var node = SVGDocument.getElementById(nodeId.value);
	var nodeAttr = node.getAttribute('nodeAttr');
	var img = 'start_click.gif';
	if('task' == nodeAttr)
	{
		img = 'task_click.gif';
	}
	else if('end' == nodeAttr)
	{
		img = 'end_click.gif';
	}
	node.setAttributeNS(xlinkNamespace, 'href', 'image/' + img);
}

function initFlow()
{
	xmlDom = loadXmlFile('<flow><nodes id="allNodes"></nodes><arrows id="allArrows"></arrows><texts id="allTexts"></texts></flow>');
	xmlDoc = xmlDom.documentElement;
	var flowType = getParent().$('#flowType');
	if('update' == flowType.val())
	{
		var content = getParent().$('#content');
		var maxId = getParent().$('#maxId');
		xmlDom = loadXmlFile(content.val());
		xmlDoc = xmlDom.documentElement;
		idSeq = parseInt(maxId.val(), 10);
		initWorkFlow();
	}
	allNodes = getOneElement('/flow/nodes[@id="allNodes"]');
	allArrows = getOneElement('/flow/arrows[@id="allArrows"]');
	allTexts = getOneElement('/flow/texts[@id="allTexts"]');
}

function grab(evt)
{
	var targetElement = evt.target;
	DragTarget = targetElement;
	addLineFirstTargetId = targetElement.getAttribute('id');
	if ( BackDrop != targetElement )
	{
		var nodeType = targetElement.getAttribute('nodeType');
		var nodeAttr = targetElement.getAttribute('nodeAttr');
		if(targetElement == null)
		{
			return;
		}
		else if('node' == nodeType)
		{
			if(arrowFlag && 'text' != nodeAttr)
			{
				var newId = 'arrow_' + getRandomId();
				var points = evt.clientX + ',' + evt.clientY + ' ' + evt.clientX + ',' + evt.clientY;
				currentArrow = createArrow(newId, points);
				SVGRoot.appendChild(currentArrow);
			}
			else
			{
				var transMatrix = DragTarget.getCTM();
				GrabPoint.x = TrueCoords.x - Number(transMatrix.e);
				GrabPoint.y = TrueCoords.y - Number(transMatrix.f);
			}
		}
		else if('line' == nodeType && !arrowFlag)
		{
			addPoint(targetElement.getAttribute('id'), evt);
		}
			
	}
}


function drag(evt)
{
	// account for zooming and panning
	GetTrueCoords(evt);
	if(arrowFlag)
	{
		if(currentArrow == null)
			return;
	 
		var points = currentArrow.getAttribute('points');
		var pointsArr = toPointsArray(points);
		var endPosition = addLineEndPosition(pointsArr[0][0], pointsArr[0][1], evt.clientX, evt.clientY);
		var newPoints = pointsArr[0][0] + ',' + pointsArr[0][1] + ' ' + (evt.clientX + endPosition.x) + ',' + (evt.clientY + endPosition.y);
		currentArrow.setAttribute('points', newPoints);
	}
	
	if (DragTarget && !arrowFlag)
	{
		var nodeType = DragTarget.getAttribute('nodeType');
		var nodeAttr = DragTarget.getAttribute('nodeAttr');
		if('node' == nodeType)
		{
			var newX = TrueCoords.x - GrabPoint.x;
			var newY = TrueCoords.y - GrabPoint.y;
			DragTarget.setAttributeNS(null, 'transform', 'translate(' + newX + ',' + newY + ')');
			var pos = getElementPosition(DragTarget);
			if('start' == nodeAttr || 'task' == nodeAttr || 'end' == nodeAttr)
			{
				allArrowMove(DragTarget);
				textMove(DragTarget.getAttribute('id'), pos.x, pos.y);
			}
			else if('text' == nodeAttr)
			{
				var targetNode = getOneElement('/flow/texts/text[@id="' + DragTarget.getAttribute('id') + '"]');
				targetNode.setAttribute('x', pos.x);
				targetNode.setAttribute('y', pos.y);
			}
		}
		else if('line' == nodeType)
		{
			var newX = evt.clientX;
			var newY = evt.clientY;
			var pointsArr = toPointsArray(DragTarget.getAttribute('points'));
			var pointsLen = pointsArr.length;
			if(addPointIndex != 0 && addPointIndex != pointsLen - 1)
			{
				var lineXml = getOneElement('/flow/arrows/lineArrow[@id="' + DragTarget.getAttribute('id') + '"]');
				var fromId = lineXml.getAttribute('from');
				var toId = lineXml.getAttribute('to');
				var fromPos = getElementPosition(SVGDocument.getElementById(fromId));
				var toPos = getElementPosition(SVGDocument.getElementById(toId));
				if(addPointIndex == 1)
				{
					var trans = getArrowFirstTrans(fromPos.x + halfLong, fromPos.y + halfLong, newX, newY);
					pointsArr[0][0] = fromPos.x + halfLong + trans.x;
					pointsArr[0][1] = fromPos.y + halfLong + trans.y;
				}
				if(addPointIndex == pointsArr.length - 2)
				{
					var trans = getArrowLastTrans(newX, newY, toPos.x + halfLong, toPos.y + halfLong);
					pointsArr[pointsLen - 1][0] = toPos.x + halfLong + trans.x;
					pointsArr[pointsLen - 1][1] = toPos.y + halfLong + trans.y;
				}
				pointsArr[addPointIndex][0] = newX;
				pointsArr[addPointIndex][1] = newY;
				var pointsStr = toPointsString(pointsArr);
				DragTarget.setAttribute('points', pointsStr);
				lineXml.setAttribute('points', pointsStr);
			}
		}
	}
}


function drop(evt)
{
	var targetElement = evt.target;
	var targetElementId = targetElement.getAttribute('id');
	var hasArrow = getElements('/flow/arrows/lineArrow[@from="' + addLineFirstTargetId + '"][@to="' + targetElementId + '"]');
	var nodeType = targetElement.getAttribute('nodeType');
	var nodeAttr = targetElement.getAttribute('nodeAttr');
	if ('node' == nodeType && addLineFirstTargetId != targetElementId && 'text' != nodeAttr)
	{
		var firstTarget = SVGDocument.getElementById(addLineFirstTargetId);
		var lastTarget = SVGDocument.getElementById(targetElementId);
		if(arrowFlag && hasArrow.length == 0)
		{
			if('start' == firstTarget.getAttribute('nodeAttr') && 'end' == lastTarget.getAttribute('nodeAttr'))
			{
				alert('不能直接从开始流转到结束！');
				if(currentArrow != null)
					SVGRoot.removeChild(currentArrow);
			}
			else if('end' == firstTarget.getAttribute('nodeAttr'))
			{
				alert('不能从结束流转到其他节点！');
				if(currentArrow != null)
					SVGRoot.removeChild(currentArrow);
			}
			else if('start' == lastTarget.getAttribute('nodeAttr'))
			{
				alert('不能从其他节点流转到开始！');
				if(currentArrow != null)
					SVGRoot.removeChild(currentArrow);
			}
			else //添加流转
			{
				var firstX = getElementPosition(firstTarget).x + halfLong;
				var firstY = getElementPosition(firstTarget).y + halfLong;
				var targetX = getElementPosition(targetElement).x + halfLong;
				var targetY = getElementPosition(targetElement).y + halfLong;
				var firstTrans = getArrowFirstTrans(firstX, firstY, targetX, targetY);
				var lastTrans = getArrowLastTrans(firstX, firstY, targetX, targetY);
				var startP = [firstX + firstTrans.x, firstY + firstTrans.y];
				var endP = [targetX + lastTrans.x, targetY + lastTrans.y];
				var finalP = [startP, endP];
				var finalPStr = toPointsString(finalP);
				currentArrow.setAttribute('points', finalPStr);
				createLineArrowXml(currentArrow.getAttribute('id'), finalPStr, addLineFirstTargetId, targetElementId);
			}
		}
		else if(arrowFlag && hasArrow.length > 0)
		{
			alert('已存在该两节点之间的流转！');
			if(currentArrow != null)
				SVGRoot.removeChild(currentArrow);
		}
	}
	else
	{
		if(currentArrow != null)
			SVGRoot.removeChild(currentArrow);
	}
	currentArrow = null;
	addLineFirstTargetId = '';
	
	if('line' == nodeType && !arrowFlag)
	{
		removePoint(DragTarget.getAttribute('id'));
	}
	
	// if we aren't currently dragging an element, don't do anything
	if ( DragTarget && !arrowFlag )
	{
		// since the element currently being dragged has its pointer-events turned off,
		// we are afforded the opportunity to find out the element it's being dropped on
	
		// turn the pointer-events back on, so we can grab this item later
		if ( 'Folder' == targetElement.parentNode.id )
		{
			// if the dragged element is dropped on an element that is a child
			// of the folder group, it is inserted as a child of that group
			//targetElement.parentNode.appendChild( DragTarget );
			//alert(DragTarget.id + ' has been dropped into a folder, and has been inserted as a child of the containing group.');
		}
		else
		{
			// for this example, you cannot drag an item out of the folder once it's in there;
			// however, you could just as easily do so here
			//alert(DragTarget.id + ' has been dropped on top of ' + targetElement.id);
		}
		
		// set the global variable to null, so nothing will be dragged until we
		// grab the next element
		DragTarget = null;
	}
}

function GetTrueCoords(evt)
{
	// find the current zoom level and pan setting, and adjust the reported
	// mouse position accordingly
	var newScale = SVGRoot.currentScale;
	var translation = SVGRoot.currentTranslate;
	TrueCoords.x = (evt.screenX - translation.x)/newScale;
	TrueCoords.y = (evt.screenY - translation.y)/newScale;
}

function allArrowMove(target)
{
	var targetPosition = getElementPosition(target);
	var id = target.getAttribute('id');
	var fromArrows = getElements('/flow/arrows/lineArrow[@from="' + id + '"]');
	var toArrows = getElements('/flow/arrows/lineArrow[@to="' + id + '"]');
	var fromLen = fromArrows.length;
	var toLen = toArrows.length;
	//SVGDocument.getElementById('testTxt').firstChild.nodeValue = fromLen;
	for(var i = 0; i < fromLen; i++)
	{
		var fromArrowx = fromArrows[i];
		var toNodeId = fromArrowx.getAttribute('to');
		var realArrowx = SVGDocument.getElementById(fromArrowx.getAttribute('id'));
		if(realArrowx == null)
			return;
		var toNodex = SVGDocument.getElementById(toNodeId);
		var toNodexPosition = getElementPosition(toNodex);
		var pointsStr = fromArrowx.getAttribute('points');
		var pointsArr = toPointsArray(pointsStr);
		var pointsLen = pointsArr.length;
		if(pointsLen == 2)
		{
			var firstTrans = getArrowFirstTrans(targetPosition.x + halfLong, targetPosition.y + halfLong, toNodexPosition.x + halfLong, toNodexPosition.y + halfLong);
			var lastTrans = getArrowLastTrans(targetPosition.x + halfLong, targetPosition.y + halfLong, toNodexPosition.x + halfLong, toNodexPosition.y + halfLong);
			var startP = [targetPosition.x + halfLong + firstTrans.x, targetPosition.y + halfLong + firstTrans.y];
			var endP = [toNodexPosition.x + halfLong + lastTrans.x, toNodexPosition.y + halfLong + lastTrans.y];
			var finalP = [startP, endP];
			var finalPStr = toPointsString(finalP);
			realArrowx.setAttribute('points', finalPStr);
			fromArrowx.setAttribute('points', finalPStr);
		}
		else if(pointsLen > 2)
		{
			var pointsStr = realArrowx.getAttribute('points');
			var pointsArr = toPointsArray(pointsStr);
			var firstTrans = getArrowFirstTrans(targetPosition.x + halfLong, targetPosition.y + halfLong, pointsArr[1][0], pointsArr[1][1]);
			var startP = [targetPosition.x + halfLong + firstTrans.x, targetPosition.y + halfLong + firstTrans.y];
			pointsArr[0] = startP;
			pointsStr = toPointsString(pointsArr);
			realArrowx.setAttribute('points', pointsStr);
			fromArrowx.setAttribute('points', pointsStr);
		}
	}
	for(var i = 0; i < toLen; i++)
	{
		var toArrowx = toArrows[i];
		var fromNodeId = toArrowx.getAttribute('from');
		var realArrowx = SVGDocument.getElementById(toArrowx.getAttribute('id'));
		if(realArrowx == null)
			return;
		var fromNodex = SVGDocument.getElementById(fromNodeId);
		var fromNodexPosition = getElementPosition(fromNodex);
		var pointsStr = toArrowx.getAttribute('points');
		var pointsArr = toPointsArray(pointsStr);
		var pointsLen = pointsArr.length;
		if(pointsLen == 2)
		{
			var firstTrans = getArrowFirstTrans(fromNodexPosition.x + halfLong, fromNodexPosition.y + halfLong, targetPosition.x + halfLong, targetPosition.y + halfLong);
			var lastTrans = getArrowLastTrans(fromNodexPosition.x + halfLong, fromNodexPosition.y + halfLong, targetPosition.x + halfLong, targetPosition.y + halfLong);
			var startP = [fromNodexPosition.x + halfLong + firstTrans.x, fromNodexPosition.y + halfLong + firstTrans.y];
			var endP = [targetPosition.x + halfLong + lastTrans.x, targetPosition.y + halfLong + lastTrans.y];
			var finalP = [startP, endP];
			var finalPStr = toPointsString(finalP);
			realArrowx.setAttribute('points', finalPStr);
			toArrowx.setAttribute('points', finalPStr);
		}
		else if(pointsLen > 2)
		{
			var pointsStr = realArrowx.getAttribute('points');
			var pointsArr = toPointsArray(pointsStr);
			var lastTrans = getArrowLastTrans(pointsArr[pointsLen - 2][0], pointsArr[pointsLen - 2][1], targetPosition.x + halfLong, targetPosition.y + halfLong);
			var lastP = [targetPosition.x + halfLong + lastTrans.x, targetPosition.y + halfLong + lastTrans.y];
			pointsArr[pointsLen - 1] = lastP;
			pointsStr = toPointsString(pointsArr);
			realArrowx.setAttribute('points', pointsStr);
			toArrowx.setAttribute('points', pointsStr);
		}
	}
	
	var targetNode = getOneElement('/flow/nodes/node[@id="' + id + '"]');
	targetNode.setAttribute('x', targetPosition.x);
	targetNode.setAttribute('y', targetPosition.y);
}

function textMove(nodeId, nodeX, nodeY)
{
	var node = getOneElement('/flow/nodes/node[@id="' + nodeId + '"]');
	var name = node.getAttribute('name');
	var textLen = name.length;
	var thisText = SVGDocument.getElementById('t_' + nodeId);
	var textPx = thisText.getComputedTextLength();
	var subPx = textPx / 2;
	thisText.setAttribute('x', nodeX + halfLong - subPx);
	thisText.setAttribute('y', nodeY + 2 * halfLong + 6);
}

function getElementPosition(target)
{
	var x = parseInt(target.getAttribute('x'), 10);
	var y = parseInt(target.getAttribute('y'), 10);
	var trans = getTransform(target);
	var result = {x:0,y:0};
	result.x = x + trans.x;
	result.y = y + trans.y;
	return result;
}

function createArrow(id, points)
{
	var arrow = SVGDocument.createElementNS(svgNamespace, 'polyline');
	arrow.setAttributeNS(null, 'points', points);
	arrow.setAttributeNS(null, 'stroke', 'black');
	arrow.setAttributeNS(null, 'style', 'cursor:ne-resize;fill:none;stroke-width:1');
	arrow.setAttributeNS(null, 'id', id);
	arrow.setAttributeNS(null, 'nodeType', 'line');
	arrow.setAttributeNS(null, 'nodeAttr', 'polyline');
	arrow.setAttributeNS(null, 'marker-end', 'url(#arrawInit)');
	arrow.setAttributeNS(null, 'onclick', 'flowArrowClick(evt, \'' + id + '\')');
	return arrow;
}

function createStart(id, x, y, name)
{
	if(name == undefined)
	{
		name = '';
	}
	var g = SVGDocument.createElementNS(svgNamespace, 'g');
	g.setAttributeNS(null, 'id', 'g_' + id);
	var img = SVGDocument.createElementNS(svgNamespace, 'image');
	img.setAttributeNS(null, 'x', x);
	img.setAttributeNS(null, 'y', y);
	img.setAttributeNS(null, 'width', 2 * halfLong);
	img.setAttributeNS(null, 'height', 2 * halfLong);
	img.setAttributeNS(null, 'id', id);
	img.setAttributeNS(xlinkNamespace, 'href', 'image/start.gif');
	img.setAttributeNS(null, 'nodeType', 'node');
	img.setAttributeNS(null, 'nodeAttr', 'start');
	img.setAttributeNS(null, 'transform', 'translate(0,0)');
	img.setAttributeNS(null, 'style', 'cursor:move');
	img.addEventListener('click', flowStartClick, false);
	
	var textLen = name.length;
	var textPx = textLen * oneTextPx;
	var subPx = textPx / 2;
	var text = createText('t_' + id, x + halfLong - subPx, y + 2 * halfLong + 6, name, false);
	g.appendChild(img);
	g.appendChild(text);
	return g;
}

function createTask(id, x, y, name)
{
	if(name == undefined)
	{
		name = '';
	}
	var g = SVGDocument.createElementNS(svgNamespace, 'g');
	g.setAttributeNS(null, 'id', 'g_' + id);
	var img = SVGDocument.createElementNS(svgNamespace, 'image');
	img.setAttributeNS(null, 'x', x);
	img.setAttributeNS(null, 'y', y);
	img.setAttributeNS(null, 'width', 2 * halfLong);
	img.setAttributeNS(null, 'height', 2 * halfLong);
	img.setAttributeNS(null, 'id', id);
	img.setAttributeNS(xlinkNamespace, 'href', 'image/task.gif');
	img.setAttributeNS(null, 'nodeType', 'node');
	img.setAttributeNS(null, 'nodeAttr', 'task');
	img.setAttributeNS(null, 'transform', 'translate(0,0)');
	img.setAttributeNS(null, 'style', 'cursor:move');
	img.addEventListener('click', flowTaskClick, false);
	
	var textLen = name.length;
	var textPx = textLen * oneTextPx;
	var subPx = textPx / 2;
	var text = createText('t_' + id, x + halfLong - subPx, y + 2 * halfLong + 6, name, false);
	g.appendChild(img);
	g.appendChild(text);
	SVGRoot.appendChild(g);
}

function createEnd(id, x, y, name)
{
	if(name == undefined)
	{
		name = '';
	}
	var g = SVGDocument.createElementNS(svgNamespace, 'g');
	g.setAttributeNS(null, 'id', 'g_' + id);
	var img = SVGDocument.createElementNS(svgNamespace, 'image');
	img.setAttributeNS(null, 'x', x);
	img.setAttributeNS(null, 'y', y);
	img.setAttributeNS(null, 'width', 2 * halfLong);
	img.setAttributeNS(null, 'height', 2 * halfLong);
	img.setAttributeNS(null, 'id', id);
	img.setAttributeNS(xlinkNamespace, 'href', 'image/end.gif');
	img.setAttributeNS(null, 'nodeType', 'node');
	img.setAttributeNS(null, 'nodeAttr', 'end');
	img.setAttributeNS(null, 'transform', 'translate(0,0)');
	img.setAttributeNS(null, 'style', 'cursor:move');
	img.addEventListener('click', flowEndClick, false);
	
	var textLen = name.length;
	var textPx = textLen * oneTextPx;
	var subPx = textPx / 2;
	var text = createText('t_' + id, x + halfLong - subPx, y + 2 * halfLong + 6, name, false);
	g.appendChild(img);
	g.appendChild(text);
	SVGRoot.appendChild(g);
}

function createText(id, x, y, desc, allowMove)
{
	if(desc == undefined || desc == null || '' == desc)
	{
		desc = ' '; //一定要有值
	}
	if(allowMove == undefined || allowMove == null)
	{
		allowMove = false;
	}
	var text = SVGDocument.createElementNS(svgNamespace, 'text');
	text.setAttribute('id', id);
	text.setAttribute('x', x);
	text.setAttribute('y', y);
	text.setAttribute('nodeAttr', 'text');
	text.setAttribute('style', 'font-size:15px');
	text.setAttribute('font-family', 'SimSun');
	text.setAttribute('fill', 'black');
	text.setAttribute('transform', 'translate(0,0)');
	text.addEventListener('click', flowTextClick, false);
	if(allowMove)
	{
		text.setAttribute('nodeType', 'node');
	}
	var textNode = SVGDocument.createTextNode(desc);
	text.appendChild(textNode);
	return text;
}

function createStartXml(id, x, y, name)
{
	if(name == undefined)
	{
		name = '';
	}
	var start = xmlDom.createElement('node');
	start.setAttribute('id', id);
	start.setAttribute('name', name);
	start.setAttribute('x', x);
	start.setAttribute('y', y);
	start.setAttribute('nodeAttr', 'start');
	start.setAttribute('status', '');
	start.setAttribute('actors', '');
	allNodes.appendChild(start);
}

function createTaskXml(id, x, y, name)
{
	if(name == undefined)
	{
		name = '';
	}
	var task = xmlDom.createElement('node');
	task.setAttribute('id', id);
	task.setAttribute('name', name);
	task.setAttribute('x', x);
	task.setAttribute('y', y);
	task.setAttribute('nodeAttr', 'task');
	task.setAttribute('status', '');
	task.setAttribute('actors', '');
	task.setAttribute('conditionType', 'or');
	task.setAttribute('assignToUser', 'false');
	task.setAttribute('permitReturn', 'true');
	task.setAttribute('actorsMode', 'select');
	task.setAttribute('percentage', '0');
	
	var auditorCode = xmlDom.createElement('auditorCode');
	var modifyCode = xmlDom.createElement('modifyCode');
	task.appendChild(auditorCode);
	task.appendChild(modifyCode);
	allNodes.appendChild(task);
}

function createEndXml(id, x, y, name)
{
	if(name == undefined)
	{
		name = '';
	}
	var end = xmlDom.createElement('node');
	end.setAttribute('id', id);
	end.setAttribute('name', name);
	end.setAttribute('x', x);
	end.setAttribute('y', y);
	end.setAttribute('nodeAttr', 'end');
	end.setAttribute('status', '');
	allNodes.appendChild(end);
}

function createTextXml(id, x, y, name)
{
	if(name == undefined)
	{
		name = '文本';
	}
	var text = xmlDom.createElement('text');
	text.setAttribute('id', id);
	text.setAttribute('name', name);
	text.setAttribute('x', x);
	text.setAttribute('y', y);
	text.setAttribute('nodeAttr', 'text');
	allTexts.appendChild(text);
}

function createLineArrowXml(id, points, from, to)
{
	var arrow = xmlDom.createElement('lineArrow');
	arrow.setAttribute('id', id);
	arrow.setAttribute('points', points);
	arrow.setAttribute('from', from);
	arrow.setAttribute('to', to);
	arrow.setAttribute('sort', '10');
	
	var condition = xmlDom.createElement('condition');
	var action = xmlDom.createElement('action');
	arrow.appendChild(condition);
	arrow.appendChild(action);
	allArrows.appendChild(arrow);
}

function startClick()
{
	changeArrowFlag('other');
	var startNode = getElements('/flow/nodes/node[@nodeAttr="start"]');
	if(startNode.length > 0)
	{
		alert('只能有一个开始节点！');
		return;
	}
	var newId = 'node_' + getRandomId();
	var x = 100, y = 100;
	var name = '开始';
	var startNode = createStart(newId, x, y, name);
	SVGRoot.appendChild(startNode);
	createStartXml(newId, x, y, name);
}

function taskClick()
{
	changeArrowFlag('other');
	var newId = 'node_' + getRandomId();
	var x = 100, y = 100;
	var name = '任务';
	createTask(newId, x, y, name);
	createTaskXml(newId, x, y, name);
}

function endClick()
{
	changeArrowFlag('other');
	var newId = 'node_' + getRandomId();
	var x = 100, y = 100;
	var name = '完成';
	createEnd(newId, x, y, name);
	createEndXml(newId, x, y, name);
}

function textClick()
{
	changeArrowFlag('other');
	var newId = 'text_' + getRandomId();
	var x = 130, y = 145;
	var desc = '文本';
	var txt = createText(newId, x, y, desc, true);
	SVGRoot.appendChild(txt);
	createTextXml(newId, x, y, desc);
}

function changeArrowFlag(type)
{
	if('self' == type)
	{
		arrowFlag = !arrowFlag;
	}
	else
	{
		arrowFlag = false;
	}
	var lineBtn = SVGDocument.getElementById('lineBtn');
	var lineBtnTxt = SVGDocument.getElementById('lineBtnTxt');
	var color = 'black';
	var arrawEnd = 'arrawInit';
	if(arrowFlag == true)
	{
		color = 'lightgreen';
		arrawEnd = 'arrawSelect';
	}
	lineBtn.setAttribute('stroke', color);
	lineBtn.setAttribute('marker-end', 'url(#' + arrawEnd + ')');
	lineBtnTxt.setAttribute('stroke', color);
}

function directionNum(x1, y1, x2, y2)
{
	var result = 0;
	 if(x2 > x1 && y2 < y1)
	{
		 result = 1;
	}
	else if(x2 > x1 && y2 > y1)
	{
		 result = 2;
	}
	else if(x2 < x1 && y2 > y1)
	{
		 result = 3;
	}
	else if(x2 < x1 && y2 < y1)
	{
		 result = 4;
	}
	else if(x2 == x1 && y2 < y1)
	{
		 result = 5;
	}
	else if(x2 > x1 && y2 == y1)
	{
		 result = 6;
	}
	else if(x2 == x1 && y2 > y1)
	{
		 result = 7;
	}
	else if(x2 < x1 && y2 == y1)
	{
		 result = 8;
	}
	return result;
}

function addLineEndPosition(x1, y1, x2, y2)
{
	var direction = directionNum(x1, y1, x2, y2);
	var result = {'x':0, 'y':0};
	var value = 2;
	if(1 == direction)
	{
		 result.x = -value;
		 result.y = value;
	}
	else if(2 == direction)
	{
		 result.x = -value;
		 result.y = -value;
	}
	else if(3 == direction)
	{
		 result.x = value;
		 result.y = -value;
	}
	else if(4 == direction)
	{
		 result.x = value;
		 result.y = value;
	}
	else if(5 == direction)
	{
		 result.x = 0;
		 result.y = value;
	}
	else if(6 == direction)
	{
		 result.x = -value;
		 result.y = 0;
	}
	else if(7 == direction)
	{
		 result.x = 0;
		 result.y = -value;
	}
	else if(8 == direction)
	{
		 result.x = value;
		 result.y = 0;
	}
	return result;
}

function getArrowFirstTrans(x1, y1, x2, y2)
{
	var direction = directionNum(x1, y1, x2, y2);
	var subX = Math.abs(x1 - x2);
	var subY = Math.abs(y1 - y2);
	var result = {x:0, y:0};
	var tranX = 0, tranY = 0;
	if(subX != 0)
	{
		var k = subY / subX;
		tranX = Math.sqrt(halfLong * halfLong / (1 + k * k));
		tranY = parseInt(tranX * k);
		tranX = parseInt(tranX);
	}
	else
	{
		tranX = 0;
		tranY = halfLong;
	}
	if(direction == 1)
	{
		result.x = tranX;
		result.y = -tranY;
	}
	else if(direction == 2)
	{
		result.x = tranX;
		result.y = tranY;
	}
	else if(direction == 3)
	{
		result.x = -tranX;
		result.y = tranY;
	}
	else if(direction == 4)
	{
		result.x = -tranX;
		result.y = -tranY;
	}
	else if(direction == 5)
	{
		result.x = 0;
		result.y = -tranY;
	}
	else if(direction == 6)
	{
		result.x = tranX;
		result.y = 0;
	}
	else if(direction == 7)
	{
		result.x = 0;
		result.y = tranY;
	}
	else if(direction == 8)
	{
		result.x = -tranX;
		result.y = 0;
	}
	return result;
}

function getArrowLastTrans(x1, y1, x2, y2)
{
	var direction = directionNum(x1, y1, x2, y2);
	var subX = Math.abs(x1 - x2);
	var subY = Math.abs(y1 - y2);
	var result = {x:0, y:0};
	var tranX = 0, tranY = 0;
	if(subX != 0)
	{
		var k = subY / subX;
		tranX = Math.sqrt(halfLong * halfLong / (1 + k * k));
		tranY = parseInt(tranX * k);
		tranX = parseInt(tranX);
	}
	else
	{
		tranX = 0;
		tranY = halfLong;
	}
	if(direction == 1)
	{
		result.x = -tranX;
		result.y = tranY;
	}
	else if(direction == 2)
	{
		result.x = -tranX;
		result.y = -tranY;
	}
	else if(direction == 3)
	{
		result.x = tranX;
		result.y = -tranY;
	}
	else if(direction == 4)
	{
		result.x = tranX;
		result.y = tranY;
	}
	else if(direction == 5)
	{
		result.x = 0;
		result.y = tranY;
	}
	else if(direction == 6)
	{
		result.x = -tranX;
		result.y = 0;
	}
	else if(direction == 7)
	{
		result.x = 0;
		result.y = -tranY;
	}
	else if(direction == 8)
	{
		result.x = tranX;
		result.y = 0;
	}
	return result;
}

function getTransform(obj)
{
	var result = {x:0, y:0};
	var transform = obj.getAttribute('transform');
	if(transform != null && '' != transform)
	{
		var leftIndex = transform.indexOf('(');
		var centerIndex = transform.indexOf(',');
		var rightIndex = transform.indexOf(')');
		var x = transform.substring(leftIndex + 1, centerIndex);
		var y = transform.substring(centerIndex + 1, rightIndex);
		result.x = parseInt(x);
		result.y = parseInt(y);
	}
	return result;
}

function getRandomId()
{
	var ranStr = getRandomStrByCount(3);
	var id = ranStr + '_' + idSeq;
	idSeq++;
	return id;
}

function getRandomStrByCount(count)
{
	var result = '';
	var arr = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e',
	           'f','g','h','i','j','k','l','m','n','o','p','q','r','s','t',
	           'u','v','w','x','y','z'];
	for(var i = 0; i < count; i++)
	{
		var digital = Math.floor(Math.random() * 35 + 1);
		result += arr[digital];
	}
	return result;
}

function flowArrowClick(evt, arrowId)
{
	cleanBeforeElement();
	var currentEle = SVGDocument.getElementById(arrowId);
	currentEle.setAttribute('marker-end', 'url(#arrawSelect)');
	currentEle.setAttribute('stroke', 'lightgreen');
	currentElementId = arrowId;
	if(evt.detail == 2)
	{
		var lineArrow = {};
		var currentNodeXml = getOneElement('/flow/arrows/lineArrow[@id="' + arrowId + '"]');
		lineArrow.id = arrowId;
		lineArrow.sort = currentNodeXml.getAttribute('sort') == null ? '10' : currentNodeXml.getAttribute('sort');
		lineArrow.conditionCode = getChildNodeText(currentNodeXml, 'condition');
		lineArrow.actionCode = getChildNodeText(currentNodeXml, 'action');
		getParent().arrowDblClick(lineArrow);
	}
}

function flowStartClick(evt)
{
	cleanBeforeElement();
	var currentEle = evt.target;
	var nodeId = currentEle.getAttribute('id');
	currentElementId = nodeId;
	currentEle.setAttributeNS(xlinkNamespace, 'href', 'image/start_click.gif');
	if(evt.detail == 2)
	{
		var startNode = {};
		var currentNodeXml = getOneElement('/flow/nodes/node[@id="' + nodeId + '"]');
		startNode.id = nodeId;
		startNode.name = currentNodeXml.getAttribute('name');
		startNode.status = currentNodeXml.getAttribute('status');
		startNode.actors = currentNodeXml.getAttribute('actors');
		getParent().startDblClick(startNode);
	}
}
function flowTaskClick(evt)
{
	cleanBeforeElement();
	var currentEle = evt.target;
	var nodeId = currentEle.getAttribute('id');
	currentElementId = nodeId;
	currentEle.setAttributeNS(xlinkNamespace, 'href', 'image/task_click.gif');
	if(evt.detail == 2)
	{
		var taskNode = {};
		var currentNodeXml = getOneElement('/flow/nodes/node[@id="' + nodeId + '"]');
		taskNode.id = nodeId;
		taskNode.name = currentNodeXml.getAttribute('name');
		taskNode.status = currentNodeXml.getAttribute('status');
		taskNode.actors = currentNodeXml.getAttribute('actors');
		taskNode.conditionType = currentNodeXml.getAttribute('conditionType');
		taskNode.assignToUser = currentNodeXml.getAttribute('assignToUser');
		taskNode.permitReturn = currentNodeXml.getAttribute('permitReturn');
		taskNode.actorsMode = currentNodeXml.getAttribute('actorsMode');
		taskNode.percentage = currentNodeXml.getAttribute('percentage');
		taskNode.auditorCode = getChildNodeText(currentNodeXml, 'auditorCode');
		taskNode.modifyCode = getChildNodeText(currentNodeXml, 'modifyCode');
		getParent().taskDblClick(taskNode);
	}
}
function flowEndClick(evt)
{
	cleanBeforeElement();
	var currentEle = evt.target;
	var nodeId = currentEle.getAttribute('id');
	currentElementId = nodeId;
	currentEle.setAttributeNS(xlinkNamespace, 'href', 'image/end_click.gif');
	if(evt.detail == 2)
	{
		var endNode = {};
		var currentNodeXml = getOneElement('/flow/nodes/node[@id="' + nodeId + '"]');
		endNode.id = nodeId;
		endNode.name = currentNodeXml.getAttribute('name');
		endNode.status = currentNodeXml.getAttribute('status');
		getParent().endDblClick(endNode);
	}
}
function flowTextClick(evt)
{
	cleanBeforeElement();
	var currentEle = evt.target;
	var nodeId = currentEle.getAttribute('id');
	currentElementId = nodeId;
	currentEle.setAttribute('fill', 'lightgreen');
	if(evt.detail == 2)
	{
		var textNode = {};
		var currentNodeXml = getOneElement('/flow/texts/text[@id="' + nodeId + '"]');
		if(currentEle.getAttribute('nodeType') == 'node')
		{
			textNode.id = nodeId;
			textNode.name = currentNodeXml.getAttribute('name');
			getParent().textDblClick(textNode);
		}
	}
}

function cleanBeforeElement()
{
	if(currentElementId == null)
		return;
	var beforeEle = SVGDocument.getElementById(currentElementId);
	if('polyline' == beforeEle.getAttribute('nodeAttr'))
	{
		beforeEle.setAttribute('marker-end', 'url(#arrawInit)');
		beforeEle.setAttribute('stroke', 'black');
	}
	else if('start' == beforeEle.getAttribute('nodeAttr'))
	{
		beforeEle.setAttributeNS(xlinkNamespace, 'href', 'image/start.gif');
	}
	else if('task' == beforeEle.getAttribute('nodeAttr'))
	{
		beforeEle.setAttributeNS(xlinkNamespace, 'href', 'image/task.gif');
	}
	else if('end' == beforeEle.getAttribute('nodeAttr'))
	{
		beforeEle.setAttributeNS(xlinkNamespace, 'href', 'image/end.gif');
	}
	else if('text' == beforeEle.getAttribute('nodeAttr'))
	{
		beforeEle.setAttribute('fill', 'black');
	}
}

function delFlowElement()
{
	if(currentElementId == null)
		return;
	var currentEle = SVGDocument.getElementById(currentElementId);
	var node = null;
	var nodeType = currentEle.getAttribute('nodeType');
	var nodeAttr = currentEle.getAttribute('nodeAttr');
	if('node' == nodeType)
	{
		if('start' == nodeAttr || 'task' == nodeAttr || 'end' == nodeAttr)
		{
			node = getOneElement('/flow/nodes/node[@id="' + currentElementId + '"]');
			currentEle = SVGDocument.getElementById('g_' + currentElementId);
			var fromArrows = getElements('/flow/arrows/lineArrow[@from="' + currentElementId + '"]');
			var toArrows = getElements('/flow/arrows/lineArrow[@to="' + currentElementId + '"]');
			var fromLength = fromArrows.length;
			var toLength = toArrows.length;
			for(var i = 0; i < fromLength; i++)
			{
				var fromAx = fromArrows[i];
				var realArrowx = SVGDocument.getElementById(fromAx.getAttribute('id'));
				SVGRoot.removeChild(realArrowx);
				allArrows.removeChild(fromAx);
			}
			for(var i = 0; i < toLength; i++)
			{
				var toAx = toArrows[i];
				var realArrowx = SVGDocument.getElementById(toAx.getAttribute('id'));
				SVGRoot.removeChild(realArrowx);
				allArrows.removeChild(toAx);
			}
			allNodes.removeChild(node);
		}
		else if('text' == nodeAttr)
		{
			text = getOneElement('/flow/texts/text[@id="' + currentElementId + '"]');
			allTexts.removeChild(text);
		}
	}
	else
	{
		node = getOneElement('/flow/arrows/lineArrow[@id="' + currentElementId + '"]');
		allArrows.removeChild(node);
	}
	SVGRoot.removeChild(currentEle);
	currentElementId = null;
}

function toXmlStr()
{
	if (window.ActiveXObject){
		return xmlDoc.xml;
	}
	else if (document.implementation && document.implementation.createDocument){
		var serializer = new XMLSerializer();
		var xmlString = serializer.serializeToString(xmlDoc);
		return xmlString;
	}
	return null;
}

function getOneElement(xpath)
{
	return xmlDoc.selectSingleNode(xpath);
}

function getElements(xpath)
{
	return xmlDoc.selectNodes(xpath);
}

function toPointsArray(points)
{
	var ps = points.split(' ');
	var len = ps.length;
	var arr = new Array();
	for(var i = 0; i < len; i++)
	{
		var xy = ps[i].split(',');
		var x = parseInt(xy[0]);
		var y = parseInt(xy[1]);
		var newPs = [x, y];
		arr.push(newPs);
	}
	return arr;
}

function toPointsString(arr)
{
	var len = arr.length;
	var result = '';
	for(var i = 0; i < len; i++)
	{
		var p = arr[i];
		result += p[0] + ',' + p[1];
		if(i < len - 1)
		{
			result += ' ';
		}
	}
	return result;
}

function addPoint(lineId, evt)
{
	var line = SVGDocument.getElementById(lineId);
	var points = line.getAttribute('points');
	var pointsArr = toPointsArray(points);
	var psLen = pointsArr.length;
	var newX = evt.clientX;
	var newY = evt.clientY;
	addPointIndex = 0;
	var needInsert = true;
	for(var i = 0; i < psLen; i++)
	{
		var p = pointsArr[i];
		var subX = Math.abs(newX - p[0]);
		var subY = Math.abs(newY - p[1]);
		if(subX <= 3 && subY <= 3)
		{
			if(i == 0 || i == (psLen - 1))
				return;
			needInsert = false;
			addPointIndex = i;
			break;
		}
	}
	if(needInsert)
	{
		var accordArr = new Array();
		for(var i = 0; i < psLen - 1; i++)
		{
			var p1 = pointsArr[i];
			var p2 = pointsArr[i + 1];
			var direction = directionNum(p1[0], p1[1], p2[0], p2[1]);
			var slopeScope = 0.5;
			if(1 == direction)
			{
				if(newX > p1[0] && newY < p1[1] && newX < p2[0] && newY > p2[1])
				{
					var slope1 = (p1[1] - p2[1]) / (p1[0] - p2[0]);
					var slope2 = (newY - p2[1]) / (newX - p2[0]);
					var slope3 = (newY - p1[1]) / (newX - p1[0]);
					var subSlope1 = Math.abs(slope1 - slope2);
					var subSlope2 = Math.abs(slope1 - slope3);
					var subSlope = subSlope1 < subSlope2 ? subSlope1 : subSlope2;
					if(subSlope <= Math.abs(slopeScope * slope1))
					{
						var accord = {index:(i+1), slope:subSlope};
						accordArr.push(accord);
					}
				}
			}
			else if(2 == direction)
			{
				if(newX > p1[0] && newY > p1[1] && newX < p2[0] && newY < p2[1])
				{
					var slope1 = (p1[1] - p2[1]) / (p1[0] - p2[0]);
					var slope2 = (newY - p2[1]) / (newX - p2[0]);
					var slope3 = (newY - p1[1]) / (newX - p1[0]);
					var subSlope1 = Math.abs(slope1 - slope2);
					var subSlope2 = Math.abs(slope1 - slope3);
					var subSlope = subSlope1 < subSlope2 ? subSlope1 : subSlope2;
					if(subSlope <= Math.abs(slopeScope * slope1))
					{
						var accord = {index:(i+1), slope:subSlope};
						accordArr.push(accord);
					}
				}
			}
			else if(3 == direction)
			{
				if(newX < p1[0] && newY > p1[1] && newX > p2[0] && newY < p2[1])
				{
					var slope1 = (p1[1] - p2[1]) / (p1[0] - p2[0]);
					var slope2 = (newY - p2[1]) / (newX - p2[0]);
					var slope3 = (newY - p1[1]) / (newX - p1[0]);
					var subSlope1 = Math.abs(slope1 - slope2);
					var subSlope2 = Math.abs(slope1 - slope3);
					var subSlope = subSlope1 < subSlope2 ? subSlope1 : subSlope2;
					if(subSlope <= Math.abs(slopeScope * slope1))
					{
						var accord = {index:(i+1), slope:subSlope};
						accordArr.push(accord);
					}
				}
			}
			else if(4 == direction)
			{
				if(newX < p1[0] && newY < p1[1] && newX > p2[0] && newY > p2[1])
				{
					var slope1 = (p1[1] - p2[1]) / (p1[0] - p2[0]);
					var slope2 = (newY - p2[1]) / (newX - p2[0]);
					var slope3 = (newY - p1[1]) / (newX - p1[0]);
					var subSlope1 = Math.abs(slope1 - slope2);
					var subSlope2 = Math.abs(slope1 - slope3);
					var subSlope = subSlope1 < subSlope2 ? subSlope1 : subSlope2;
					if(subSlope <= Math.abs(slopeScope * slope1))
					{
						var accord = {index:(i+1), slope:subSlope};
						accordArr.push(accord);
					}
				}
			}
			else if(5 == direction)
			{
				var subX = Math.abs(newX - p1[0]);
				if(newY < p1[1] && newY > p2[1] && subX < 2)
				{
					addPointIndex = i + 1;
					break;
				}
			}
			else if(6 == direction)
			{
				var subY = Math.abs(newY - p1[1]);
				if(newX > p1[0] && newX < p2[0] && subY < 2)
				{
					addPointIndex = i + 1;
					break;
				}
			}
			else if(7 == direction)
			{
				var subX = Math.abs(newX - p1[0]);
				if(newY > p1[1] && newY < p2[1] && subX < 2)
				{
					addPointIndex = i + 1;
					break;
				}
			}
			else if(8 == direction)
			{
				var subY = Math.abs(newY - p1[1]);
				if(newX < p1[0] && newX > p2[0] && subY < 2)
				{
					addPointIndex = i + 1;
					break;
				}
			}
		}
		var accordLen = accordArr.length;
		var minSlope = 100;
		for(var i = 0; i < accordLen; i++)
		{
			if(accordArr[i].slope < minSlope)
			{
				addPointIndex = accordArr[i].index;
				minSlope = accordArr[i].slope;
			}
		}
		if(addPointIndex != 0)
		{
			var newArr = insertPoint(pointsArr, addPointIndex, [newX, newY]);
			var pointStr = toPointsString(newArr);
			line.setAttribute('points', pointStr);
		}
	}
}

function removePoint(lineId)
{
	var line = SVGDocument.getElementById(lineId);
	var lineXml = getOneElement('/flow/arrows/lineArrow[@id="' + lineId + '"]');
	var pointsStr = line.getAttribute('points');
	var pointsArr = toPointsArray(pointsStr);
	var psLen = pointsStr.length;
	if(addPointIndex != 0 && addPointIndex != psLen - 1 && psLen > 2)
	{
		var p1 = pointsArr[addPointIndex - 1];
		var p2 = pointsArr[addPointIndex + 1];
		var currentP = pointsArr[addPointIndex];
		var needRemove = false;

		if(p1[0] != p2[0])
		{
			var slope = (p1[1] - p2[1]) / (p1[0] - p2[0]);
			var atan = Math.atan(slope);
			var yp1 = p1[0] * Math.sin(atan) - p1[1] * Math.cos(atan);
			var ypc = currentP[0] * Math.sin(atan) - currentP[1] * Math.cos(atan);
			var length = Math.abs(yp1 - ypc);
			if(length < 8)
			{
				needRemove = true;
			}
		}
		else
		{
			var subX = Math.abs(currentP[0] - p1[0]);
			if(subX < 8)
			{
				needRemove = true;
			}
		}

		if(needRemove == true)
		{
			var newArr = deletePoint(pointsArr, addPointIndex);
			var newArrStr = toPointsString(newArr);
			line.setAttribute('points', newArrStr);
			lineXml.setAttribute('points', newArrStr);
		}
	}
}

function insertPoint(arr, w, p)
{
	var j = 0;
	var psLen = arr.length;
	var newArr = new Array();
	for(var i = 0; i < psLen + 1; i++)
	{
		if(i == w)
		{
			newArr.push(p);
		}
		else
		{
			newArr.push(arr[j]);
			j++;
		}
	}
	return newArr;
}

function deletePoint(arr, w)
{
	var psLen = arr.length;
	var newArr = new Array();
	for(var i = 0; i < psLen; i++)
	{
		if(i != w)
		{
			newArr.push(arr[i]);
		}
	}
	return newArr;
}

//保存开始节点
function saveStart(startNode)
{
	var startNodeXml = getOneElement('/flow/nodes/node[@id="' + startNode.id + '"]');
	if(startNodeXml != null)
	{
		startNodeXml.setAttribute('status', startNode.status);
		startNodeXml.setAttribute('name', startNode.name);
		startNodeXml.setAttribute('actors', startNode.actors);
		SVGDocument.getElementById('t_' + startNode.id).firstChild.nodeValue = startNode.name;
		var node = SVGDocument.getElementById(startNode.id);
		var pos = getElementPosition(node);
		textMove(startNode.id, pos.x, pos.y);
	}
}

//保存任务节点
function saveTask(taskNode)
{
	var taskNodeXml = getOneElement('/flow/nodes/node[@id="' + taskNode.id + '"]');
	if(taskNodeXml != null)
	{
		taskNodeXml.setAttribute('status', taskNode.status);
		taskNodeXml.setAttribute('name', taskNode.name);
		taskNodeXml.setAttribute('actors', taskNode.actors);
		taskNodeXml.setAttribute('actorsMode', taskNode.actorsMode);
		setChildNodeText(taskNodeXml, 'auditorCode', taskNode.auditorCode);
		taskNodeXml.setAttribute('assignToUser', taskNode.assignToUser);
		taskNodeXml.setAttribute('conditionType', taskNode.conditionType);
		taskNodeXml.setAttribute('percentage', taskNode.percentage);
		taskNodeXml.setAttribute('permitReturn', taskNode.permitReturn);
		setChildNodeText(taskNodeXml, 'modifyCode', taskNode.modifyCode);
		SVGDocument.getElementById('t_' + taskNode.id).firstChild.nodeValue = taskNode.name;
		var node = SVGDocument.getElementById(taskNode.id);
		var pos = getElementPosition(node);
		textMove(taskNode.id, pos.x, pos.y);
	}
}

//保存结束节点
function saveEnd(endNode)
{
	var endNodeXml = getOneElement('/flow/nodes/node[@id="' + endNode.id + '"]');
	if(endNodeXml != null)
	{
		endNodeXml.setAttribute('status', endNode.status);
		endNodeXml.setAttribute('name', endNode.name);
		SVGDocument.getElementById('t_' + endNode.id).firstChild.nodeValue = endNode.name;
		var node = SVGDocument.getElementById(endNode.id);
		var pos = getElementPosition(node);
		textMove(endNode.id, pos.x, pos.y);
	}
}

//保存文本
function saveText(textNode)
{
	var textNodeXml = getOneElement('/flow/texts/text[@id="' + textNode.id + '"]');
	if(textNodeXml != null)
	{
		textNodeXml.setAttribute('name', textNode.name);
		SVGDocument.getElementById(textNode.id).firstChild.nodeValue = textNode.name;
	}
}

//保存流转
function saveArrow(lineArrow)
{
	var lineArrowXml = getOneElement('/flow/arrows/lineArrow[@id="' + lineArrow.id + '"]');
	if(lineArrowXml != null)
	{
		lineArrowXml.setAttribute('sort', lineArrow.sort);
		setChildNodeText(lineArrowXml, 'condition', lineArrow.conditionCode);
		setChildNodeText(lineArrowXml, 'action', lineArrow.actionCode);
	}
}

function initWorkFlow()
{
	var allNodes = getElements('/flow/nodes/node');
	var allArrows = getElements('/flow/arrows/lineArrow');
	var allTexts = getElements('/flow/texts/text');
	var nodeLen = allNodes.length;
	var arrowLen = allArrows.length;
	var textLen = allTexts.length;
	for(var i = 0; i < nodeLen; i++)
	{
		var nodex = allNodes[i];
		var id = nodex.getAttribute('id');
		var x = parseInt(nodex.getAttribute('x'), 10);
		var y = parseInt(nodex.getAttribute('y'), 10);
		var name = nodex.getAttribute('name');
		var nodeAttr = nodex.getAttribute('nodeAttr');
		if('start' == nodeAttr)
		{
			var startNode = createStart(id, x, y, name);
			SVGRoot.appendChild(startNode);
		}
		else if('task' == nodeAttr)
		{
			createTask(id, x, y, name);
		}
		else if('end' == nodeAttr)
		{
			createEnd(id, x, y, name);
		}
	}
	for(var i = 0; i < arrowLen; i++)
	{
		var arrowx = allArrows[i];
		var id = arrowx.getAttribute('id');
		var points = arrowx.getAttribute('points');
		var newArrow = createArrow(id, points);
		SVGRoot.appendChild(newArrow);
	}
	for(var i = 0; i < textLen; i++)
	{
		var textx = allTexts[i];
		var id = textx.getAttribute('id');
		var x = parseInt(textx.getAttribute('x'), 10);
		var y = parseInt(textx.getAttribute('y'), 10);
		var desc = textx.getAttribute('name');
		var txt = createText(id, x, y, desc, true);
		SVGRoot.appendChild(txt);
	}
}

//保存流程
function saveFlow()
{
	var xml = toXmlStr();
	getParent().saveFlow(xml, idSeq);
}

function getChildNodeText(xmlNode, childName)
{
	var child = xmlNode.getElementsByTagName(childName)[0].firstChild;
	if(child == null)
	{
		return '';
	}
	return child.nodeValue;
}

function setChildNodeText(xmlNode, childName, text)
{
	var child = xmlNode.getElementsByTagName(childName)[0];
	if(child.firstChild == null)
	{
		var textNode = xmlDom.createTextNode(childName);
		textNode.nodeValue = text;
		child.appendChild(textNode);
	}
	else
	{
		child.firstChild.nodeValue = text;
	}
}





